package nero.yncc.com.createart.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.utils.CacheUtil;
import nero.yncc.com.createart.utils.DialogUtil;

@ContentView(R.layout.activity_setting)
public class ActivitySetting extends BaseActivity {


    @ViewInject(R.id.tv_myset_cacheinfo)
    TextView tvCacheinfo;
    @ViewInject(R.id.ll_cache)
    LinearLayout ll_cache;
    @ViewInject(R.id.ll_changePhone)
    LinearLayout ll_changePhone;

    @ViewInject(R.id.btn_myset_logout)
    Button btnMysetLogout;
    @ViewInject(R.id.ll_changePwd)
    LinearLayout ll_changePwd;


    @Override
    protected void initView() {
        setTitleBarTitle("设置");
        getCacheSize();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        ll_cache.setOnClickListener(this);
        ll_changePwd.setOnClickListener(this);
        btnMysetLogout.setOnClickListener(this);
        ll_changePhone.setOnClickListener(this);
    }

    @Override
    protected void doClick(int id) {
        switch (id) {
            case R.id.ll_cache:
                if(cacheSize.equals("0k")){
                    showToast("现在不需要清理哦～");
                    return;
                }
                DialogUtil.getConfirmDialog(mContext, "是否清除缓存？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        CacheUtil.clearAllCache(mContext);
                        showProgress("正在清除缓存...");
                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        showToast("清除缓存成功");
                                        hideProgress();
                                        getCacheSize();
                                    }
                                });
                            }
                        },1500);

                    }
                }).show();
                break;
            case R.id.ll_changePwd:
//                startActivity(new Intent(MySetting.this, ChangePD.class));
                break;
            case R.id.btn_myset_logout:
                exitLogin();
                break;
            case R.id.ll_changePhone:
//                startActivity(new Intent(MySetting.this, AboutUs.class));
                break;
        }
    }

    String cacheSize="";
    /**
     * 获取缓存信息
     */
    private void getCacheSize() {

        File cacheDir = getCacheDir();
        try {
            long cacheSize1 = CacheUtil.getFolderSize(cacheDir);
            cacheSize = CacheUtil.getFormatSize(cacheSize1);
            tvCacheinfo.setText(cacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
    }
}
